/****************************************************************************
** ui.h extension file, included from the uic-generated form implementation.
**
** If you want to add, delete, or rename functions or slots, use
** Qt Designer to update this file, preserving your code.
**
** You should not define a constructor or destructor in this file.
** Instead, write your code in functions called init() and destroy().
** These will automatically be called by the form's constructor and
** destructor.
*****************************************************************************/

#include <qsqlquery.h>
#include <qsqlrecord.h>
#include <qsqlresult.h>
#include <qmessagebox.h>
#include <qtextcodec.h>
#include <qsqlrecord.h>
#include <qsqlselectcursor.h>
#include <qfiledialog.h>

#include <iostream>
using namespace std;


void Form4::query()
{

   QSqlSelectCursor* cur = new QSqlSelectCursor( text_query->text() );
   dataTableQuery->setSqlCursor( cur, TRUE, TRUE );
   dataTableQuery->refresh();
}

void Form4::getSQLFileName()
{
    QFileDialog* fd = new QFileDialog( this, "file dialog", TRUE );
    fd->setMode( QFileDialog::ExistingFile );
    fd->setFilter( "All Files (*.*)" );
    fd->addFilter( "SQL Files (*.sql)" );
    
    QString fileName;
    
    if ( fd->exec() == QDialog::Accepted ) {
	fileName = fd->selectedFile();
    }
    
    cout << "Reading From:  " << fileName << endl;
    
    QFile savefile(fileName);

    if (!QFile::exists(fileName)) {
	return;
    }

    if ( !savefile.open(IO_ReadOnly) ) {
	QMessageBox::warning(this, tr("Open failed"), tr("Warning: Could not open file: %1").arg(savefile.errorString()) );
	return;
    }
  
    QTextStream stream( &savefile );
    QString line;

    while ( !stream.atEnd() ) {
	line.append(stream.readLine()).append("\n");
    }
    savefile.close();
    text_query->clear();
    text_query->setText(line);
}

